//
//  CTResetPasswordViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTResetPasswordViewController.h"

//logic
#import "CTResetPasswordLogic.h"

//view
#import "CTRestPasswordView.h"

//function
#import "CTDebugManager.h"
#import "CTRemindView.h"
#import "CTTextManager.h"
#import "CTAppLanguage.h"
#import "CTActivitorView.h"
#import "CTReachability.h"
#import "CTGenRequestData.h"
#import "MobClick.h"
//tool
#import "CRSA.h"


@interface CTResetPasswordViewController ()<CTRestPasswordViewDelegate>
{
    CTRestPasswordView * resetPasswordView;
    CTResetPasswordLogic * resetPasswordLogic;
    
    CTActivitorView * acView;
    
    NSString * passwordStr;
    NSString * resetPasswordStr;
}
@end


@implementation CTResetPasswordViewController
@synthesize phoneNumber;

- (void)dealloc
{
    resetPasswordLogic = nil;
    
    [resetPasswordView removeFromSuperview];
    resetPasswordView = nil;
    
    [acView removeFromSuperview];
    acView = nil;
    
    passwordStr = nil;
    resetPasswordStr = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTResetPasswordViewController Is Removed!!!"];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        resetPasswordLogic = [[CTResetPasswordLogic alloc] init];
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    //统计
    [MobClick beginLogPageView:@"CTResetPasswordViewController"];
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    //统计
    [MobClick endLogPageView:@"CTResetPasswordViewController"];
    [super viewWillDisappear:animated];
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    
    resetPasswordView = [[CTRestPasswordView alloc] init];
    resetPasswordView.delegate = self;
    [self.view addSubview:resetPasswordView];
}





#pragma mark - view delegate

-(void) thirtPartyRegisterViewPasswordTextFieldText:(NSString *)text  registerView:(CTRestPasswordView *)registerView
{
    passwordStr = text;
}
-(void) thirtPartyRegisterViewRestPasswordTextFieldText:(NSString *)text  registerView:(CTRestPasswordView *)registerView
{
    resetPasswordStr = text;
}
//返回按钮
-(void) thirtPartyRegisterViewBackButton:(CTRestPasswordView *)registerView
{
    [self.navigationController popViewControllerAnimated:YES];
}
//完成
-(void) thirtPartyRegisterViewFinishButton:(CTRestPasswordView *)registerView
{
    
    if ([[CTTextManager shareInstance] isBlankString:passwordStr] == YES ) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_d" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([[CTTextManager shareInstance] isBlankString:resetPasswordStr] == YES ) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_d" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if (![passwordStr isEqualToString:resetPasswordStr]) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_k" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if (passwordStr.length < 6 || passwordStr.length > 14) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"logInView_PasswordDescription_a" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([[CTTextManager shareInstance] isMatch:passwordStr] == NO) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"logInView_PasswordDescription_b" withValue:nil withTable:@"Language"]];
        
        return;
    }
    
    
    //监测网络
    if ([self checkNetworkState] == NO)return;
    
    
    [self showRemindView];
    
    
    //请求
    
    [resetPasswordLogic resetPasswordRegisterRequest:[CTGenRequestData genRestPasswordRequestItemPhoneNumber:phoneNumber withPassword:passwordStr] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeActivitorView];
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_m" withValue:nil withTable:@"Language"]];
            //推到rootVC
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeActivitorView];
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_l" withValue:nil withTable:@"Language"]];
        });
    }];
    
}





#pragma mark - main thread -

-(void)showRemindView
{
    acView = [[CTActivitorView alloc] init];
    [acView showActivitorViewOn:self.view];
    
}

#pragma mark - check net work state -
-(BOOL)checkNetworkState
{
    NSString * descri;
    CTRemindView * remindView;
    
    CTNetWorkReachabilityStatus  status = [CTReachability judegeCurrentNetWorkStatus];
    switch (status) {
        case CTNetWorkReachabilityStatusUnknown:
            descri = [CTAppLanguage localizedStringForKey:@"remindView_login_networkUnknow" withValue:nil withTable:@"Language"];
            remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:descri];
            return NO;
            break;
        case CTNetWorkReachabilityStatusNotReachable:
            descri = [CTAppLanguage localizedStringForKey:@"remindView_login_networkReachable" withValue:nil withTable:@"Language"];
            remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:descri];
            return NO;
            break;
        default:
            break;
    }
    return YES;
}


-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [CTDebugManager showDebugLogInfo:@"CTResetPasswordViewController内存警告!!!"];
}


@end
